using System;
using System.Collections;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.ComponentModel.Design.Serialization;
using System.Drawing;
using System.Drawing.Design;
using System.Data;
using System.Windows.Forms;
using System.IO;
using MedicalSystem.SamrtReport.Loader;
using MedicalSystem.SmartReport.Host.Mobile;


namespace MedicalSystem.SmartReport.Host
{
	public enum LoaderType
	{
		WinFormDesignerLoader = 1,
        VerticalMobileDesignerLoader = 2,
        HorizontalMobileDesignerLoader=3,
		NoLoader = 4
	}

    /// <summary>
    /// Manages numerous HostSurfaces. Any services added to HostSurfaceManager
    /// will be accessible to all HostSurfaces
    /// </summary>
	public class HostSurfaceManager : DesignSurfaceManager
	{
		public HostSurfaceManager() : base()
		{
			this.AddService(typeof(INameCreationService), new NameCreationService());
            this.ActiveDesignSurfaceChanged += new ActiveDesignSurfaceChangedEventHandler(HostSurfaceManager_ActiveDesignSurfaceChanged);
            
		}

		protected override DesignSurface CreateDesignSurfaceCore(IServiceProvider parentProvider)
		{
			return new HostSurface(parentProvider);
		}

        /// <summary>
        /// Gets a new HostSurface and loads it with the appropriate type of
        /// root component. 
        /// </summary>
        private HostControl GetNewHost(Type rootComponentType)
		{
			HostSurface hostSurface = (HostSurface)this.CreateDesignSurface(this.ServiceContainer);

			if (rootComponentType == typeof(Form))
				hostSurface.BeginLoad(typeof(Form));
            else
				throw new Exception("Undefined Host Type: " + rootComponentType.ToString());

			hostSurface.Initialize();
			this.ActiveDesignSurface = hostSurface;
			return new HostControl(hostSurface);
		}

        /// <summary>
        /// Gets a new HostSurface and loads it with the appropriate type of
        /// root component. Uses the appropriate Loader to load the HostSurface.
        /// </summary>
        public UserControl GetNewHost(Type rootComponentType, LoaderType loaderType)
		{
			if (loaderType == LoaderType.NoLoader)
				return GetNewHost(rootComponentType);

			HostSurface hostSurface = (HostSurface)this.CreateDesignSurface(this.ServiceContainer);

            BasicHostLoader basicHostLoader = new BasicHostLoader(rootComponentType);
            hostSurface.BeginLoad(basicHostLoader);
            hostSurface.Loader = basicHostLoader;
            hostSurface.Initialize();
		   if(loaderType== LoaderType.WinFormDesignerLoader)
           {
               	return new HostControl(hostSurface);
           }
		   else if(loaderType==LoaderType.VerticalMobileDesignerLoader)
           {
               return new MobileHostControl(hostSurface);
           }
           else
           {
                return new HorizontalMobileHostControl(hostSurface);
           }
          	
		}

        /// <summary>
        /// Opens an Xml file and loads it up using BasicHostLoader (inherits from
        /// BasicDesignerLoader)
        /// </summary>
		public UserControl GetNewHost(string fileName,LoaderType loaderType,string bindingSourceName)
		{
			if (fileName == null || !File.Exists(fileName))
				MessageBox.Show("FileName is incorrect: " + fileName);		

			HostSurface hostSurface = (HostSurface)this.CreateDesignSurface(this.ServiceContainer);

            BasicHostLoader basicHostLoader = new BasicHostLoader(fileName, bindingSourceName);
            hostSurface.BeginLoad(basicHostLoader);
            hostSurface.Loader = basicHostLoader;
    		hostSurface.Initialize();
            if (loaderType == LoaderType.WinFormDesignerLoader)
            {
                return new HostControl(hostSurface);
            }
            else if (loaderType == LoaderType.VerticalMobileDesignerLoader)
            {
                return new MobileHostControl(hostSurface);
            }
            else
            {
                return new HorizontalMobileHostControl(hostSurface);
            }
		}

		public void AddService(Type type, object serviceInstance)
		{
			this.ServiceContainer.AddService(type, serviceInstance);
		}

        /// <summary>
        /// Uses the OutputWindow service and writes out to it.
        /// </summary>
		void HostSurfaceManager_ActiveDesignSurfaceChanged(object sender, ActiveDesignSurfaceChangedEventArgs e)
		{
            
		}
	}// class
}// namespace
